Page({
  data: {
    isDarkMode: false,
    themeClass: 'light',
    themeGradient: ''
  },

  // 切换主题
  toggleTheme(e) {
    const isDark = e.detail.value
    const theme = isDark ? 'dark' : 'light'
    
    // 更新全局主题
    const app = getApp()
    app.setTheme(theme)
    
    // 保存到本地存储
    wx.setStorageSync("theme", theme)
    
    // 更新页面数据
    this.setData({
      isDarkMode: isDark,
      themeClass: theme,
      themeGradient: app.globalData.themes[theme].gradient
    })
  },

  // 跳转到账号管理页面
  navigateToAccount() {
    wx.navigateTo({ url: "/pages/profile/edit/edit" });
  },

  // 跳转到关于页面
  navigateToAbout() {
    wx.navigateTo({ url: "/pages/about/about" });
  },

  // 退出登录
  logout() {
    wx.showModal({
      title: "确认退出",
      content: "您确定要退出登录吗？",
      success(res) {
        if (res.confirm) {
          wx.clearStorageSync();
          wx.reLaunch({ url: "/pages/login/login" });
        }
      },
    });
  },

  // 页面加载时读取主题并应用
  onLoad() {
    const app = getApp()
    const savedTheme = wx.getStorageSync("theme") || 'light'
    app.setTheme(savedTheme)
    this.setData({
      isDarkMode: savedTheme === 'dark',
      themeClass: savedTheme,
      themeGradient: app.globalData.themes[savedTheme].gradient
    })
  },
});
